
#include "usart.h"

#include "stdio.h"
#include "msp.h"
#include "DMA.h"

extern uint8_t V7data[50];	  //发送数据缓存


uint8_t USART1_Buf[36]={0};//USART1 DMA使用

uint8_t USART_DMABuf[2]={0};//USART1 DMA使用

/**
 * @brief   USART1初始化，用于激光模块
 * @note   
 **/
void Usart1_Init(uint32_t baudrate)
{
    //开GPIOA时钟
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
    //GPIO初始化结构体定义
    GPIO_InitTypeDef GPIO_InitStructure;
    //USART1_TX
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
    GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
    GPIO_Init(GPIOA,&GPIO_InitStructure);
    //USART1_RX
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
    GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;//GPIO_PuPd_UP
    GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
    GPIO_Init(GPIOA,&GPIO_InitStructure);

    //开USART1时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
    //USART初始化结构体定义
    USART_InitTypeDef USART_InitStructure;
    USART_InitStructure.USART_BaudRate=baudrate;//460800
    USART_InitStructure.USART_WordLength=USART_WordLength_8b;
    USART_InitStructure.USART_StopBits=USART_StopBits_1;
    USART_InitStructure.USART_Parity=USART_Parity_No;
    USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
    USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
    USART_Init(USART1,&USART_InitStructure);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        


    //开USART1空闲中断
//    USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);
	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);

//    //DMA1时钟使能
//    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);
//    DMA_DeInit(DMA2_Stream2);
//	
//    //DMA初始化结构体定义
//    DMA_InitTypeDef DMA_InitStructure;
//    //USART1_RX
//    DMA_InitStructure.DMA_Channel=DMA_Channel_4;//通道4数据流2是USART1_RX的映射
//    DMA_InitStructure.DMA_PeripheralBaseAddr=(uint32_t)&USART1->DR;//外设基地址，就是UART4的DR的地址
//    DMA_InitStructure.DMA_Memory0BaseAddr=(uint32_t)USART_DMABuf;
//    DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralToMemory;
//    DMA_InitStructure.DMA_BufferSize=sizeof(USART_DMABuf);//要发送的数据的长度
//    DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
//    DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;
//    DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Byte;
//    DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_Byte;
//    DMA_InitStructure.DMA_Mode=DMA_Mode_Normal;
//    DMA_InitStructure.DMA_Priority=DMA_Priority_VeryHigh;
//    DMA_InitStructure.DMA_FIFOMode=DMA_FIFOMode_Disable;
//    DMA_Init(DMA2_Stream2,&DMA_InitStructure);

//    //开启DMA
//    DMA_Cmd(DMA2_Stream2,ENABLE);

//    //USART1使能
//    USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
    USART_Cmd(USART1,ENABLE);
	
}





//串口二初始化,用于优像光流
void Usart2_Init(uint32_t baudrate)
{			    
  GPIO_InitTypeDef  GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;
	
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //GPIOA 外部时钟初始化   
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //USART2 时钟始终初始化
	
  //连接 USART2 的通道到 AF7
  GPIO_PinAFConfig(GPIOD,GPIO_PinSource5,GPIO_AF_USART2);  
  GPIO_PinAFConfig(GPIOD,GPIO_PinSource6,GPIO_AF_USART2); 

  USART_DeInit(USART2); //USART2 复位 
	
  //复用模式 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   
  //PB11 配置成推完输出 	RX
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      
  GPIO_Init(GPIOD, &GPIO_InitStructure);
  //PB10 配置成上拉输入   TX
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;        
  GPIO_Init(GPIOD, &GPIO_InitStructure);
	
	//串口1初始化
  USART_InitStructure.USART_BaudRate = baudrate; //波特率
  USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长八位
  USART_InitStructure.USART_StopBits = USART_StopBits_1; //一个停止位
  USART_InitStructure.USART_Parity = USART_Parity_No; //无校验位
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件流控制
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //发送接收模式
  USART_Init(USART2,&USART_InitStructure);
	
  USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); //串口接收中断

  USART_Cmd(USART2,ENABLE); //USART2使能
  
}




//串口三初始化,用于匿名上位机
void Usart3_Init(uint32_t baudrate)
{
	// 打开串口GPIO的时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	
	// 打开串口外设的时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);  	

    //GPIO初始化结构体定义
    GPIO_InitTypeDef GPIO_InitStructure;

	//USART3_TX
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
    GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3);
	
//    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
//    GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
//    GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
//    GPIO_Init(GPIOB,&GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Speed=GPIO_High_Speed;
    GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
    GPIO_Init(GPIOB,&GPIO_InitStructure);
	
    //USART3_RX
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
    GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3);
	
//    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
//    GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;//GPIO_PuPd_UP
//    GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
//    GPIO_Init(GPIOB,&GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Speed=GPIO_High_Speed;
    GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;//GPIO_PuPd_UP
    GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
    GPIO_Init(GPIOB,&GPIO_InitStructure);
	
	
	USART_DeInit(USART3); //USART3复位 
	
    //USART初始化结构体定义
    USART_InitTypeDef USART_InitStructure;
    USART_InitStructure.USART_BaudRate=baudrate;//9600
    USART_InitStructure.USART_WordLength=USART_WordLength_8b;
    USART_InitStructure.USART_StopBits=USART_StopBits_1;
    USART_InitStructure.USART_Parity=USART_Parity_No;
	USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
    USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
    USART_Init(USART3,&USART_InitStructure);
	
	USART_ITConfig(USART3,USART_IT_RXNE,ENABLE); //串口接收中断
    USART_Cmd(USART3,ENABLE);
	
	MYDMA_Config(DMA1_Stream3,DMA_Channel_4,(u32)&USART3->DR,(uint32_t)V7data,50,1,0);
		
	USART_DMACmd(USART3,USART_DMAReq_Tx,ENABLE);
	
}


//UWB
void Usart4_Init(uint32_t baudrate)
{
	GPIO_InitTypeDef  GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //GPIOA 外部时钟初始化   
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); //UART4 时钟始终初始化

	//连接 USART2 的通道到 AF7
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_UART4);  
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_UART4); 

	USART_DeInit(UART4); //USART2 复位 
	//PA0 TX4 

	//复用模式 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   
	//PB11 配置成推完输出 	RX
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	//PB10 配置成上拉输入   TX
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;        
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	//串口4初始化
	USART_InitStructure.USART_BaudRate = baudrate; //波特率
	USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长八位
	USART_InitStructure.USART_StopBits = USART_StopBits_1; //一个停止位
	USART_InitStructure.USART_Parity = USART_Parity_No; //无校验位
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件流控制
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //发送接收模式
	USART_Init(UART4,&USART_InitStructure);

	USART_ITConfig(UART4,USART_IT_RXNE,ENABLE); //串口接收中断
	//USART_ITConfig(UART4,USART_IT_IDLE,ENABLE);

	//USART_Cmd(UART4,ENABLE); //USART3使能

}



//串口五初始化,用于KS103超声波传感器
void Usart5_Init(uint32_t baudrate)
{
	
	NVIC_InitTypeDef  NVIC_InitStructure;
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断分组2
//以后打算用它的时候就把原超声波外部中断删掉，记得要调整优先级（为不影响当前其他代码现在这个优先级是最低的）
	NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢断优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//子优选级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_InitTypeDef USART_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); //开启USART2时钟	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE); 
	
	USART_DeInit(UART5); //UART5复位 
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5);
	GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5);
		
	//配置PC12作为UART5　Tx
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
	GPIO_Init(GPIOC, &GPIO_InitStructure); 
	//配置PD2作为UART5　Rx
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
	GPIO_Init(GPIOD, &GPIO_InitStructure); 
	
	//配置UART5
	//中断被屏蔽了
	USART_InitStructure.USART_BaudRate = baudrate;       //波特率可以通过地面站配置
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;  //8位数据
	USART_InitStructure.USART_StopBits = USART_StopBits_1;   //在帧结尾传输1个停止位
	USART_InitStructure.USART_Parity = USART_Parity_No;    //禁用奇偶校验
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //硬件流控制失能
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;  //发送、接收使能
	USART_Init(UART5, &USART_InitStructure);
	
	//使能UART5接收中断
	USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);
	//使能USART5
	USART_Cmd(UART5, ENABLE); 

}

//串口五初始化,用于超声波传感器
void Usart6_Init(uint32_t baudrate)
{
	
	NVIC_InitTypeDef  NVIC_InitStructure;
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断分组2
//以后打算用它的时候就把原超声波外部中断删掉，记得要调整优先级（为不影响当前其他代码现在这个优先级是最低的）
	NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢断优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//子优选级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_InitTypeDef USART_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); //开启USART2时钟
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);	

	
	USART_DeInit(USART6); //USART6复位 
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);
		
	//配置PC12作为USART6　Tx
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
	GPIO_Init(GPIOC, &GPIO_InitStructure); 
	//配置PD2作为USART6　Rx
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
	GPIO_Init(GPIOC, &GPIO_InitStructure); 
	
	//配置USART6
	//中断被屏蔽了
	USART_InitStructure.USART_BaudRate = baudrate;       //波特率可以通过地面站配置
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;  //8位数据
	USART_InitStructure.USART_StopBits = USART_StopBits_1;   //在帧结尾传输1个停止位
	USART_InitStructure.USART_Parity = USART_Parity_No;    //禁用奇偶校验
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //硬件流控制失能
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;  //发送、接收使能
	USART_Init(USART6, &USART_InitStructure);
	
	//使能USART6接收中断
	USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);
	//使能USART5
	USART_Cmd(USART6, ENABLE); 

}

/*****************  发送一定长度的数据 **********************/
void Usart_Send(uint8_t *data, uint8_t length)
{
	uint8_t  i;
    for(i=0;i<length;i++)
    {
        USART_SendData(USART3, *(data+i));
//        while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
//        {}
			
		while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
    }
	

}
/*****************  发送一个字节 **********************/
void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch)
{
	/* 发送一个字节数据到USART */
	USART_SendData(pUSARTx,ch);
		
	/* 等待发送数据寄存器为空 */
	while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);	
}

/****************** 发送8位的数组 ************************/
void Usart_SendArray( USART_TypeDef * pUSARTx, uint8_t *array, uint16_t num)
{
  uint8_t i;
	
	for(i=0; i<num; i++)
  {
	    /* 发送一个字节数据到USART */
	    Usart_SendByte(pUSARTx,array[i]);	
  
  }
	/* 等待发送完成 */
	while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET);
}

/*****************  发送字符串 **********************/
void Usart_SendString( USART_TypeDef * pUSARTx, char *str)
{
	unsigned int k=0;
  do 
  {
      Usart_SendByte( pUSARTx, *(str + k) );
      k++;
  } while(*(str + k)!='\0');
  
  /* 等待发送完成 */
  while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET)
  {}
}

/*****************  发送一个16位数 **********************/
void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch)
{
	uint8_t temp_h, temp_l;
	
	/* 取出高八位 */
	temp_h = (ch&0XFF00)>>8;
	/* 取出低八位 */
	temp_l = ch&0XFF;
	
	/* 发送高八位 */
	USART_SendData(pUSARTx,temp_h);	
	while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
	
	/* 发送低八位 */
	USART_SendData(pUSARTx,temp_l);	
	while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);	
}

///重定向c库函数printf到串口，重定向后可使用printf函数
int fputc(int ch, FILE *f)
{
		/* 发送一个字节数据到串口 */
		USART_SendData(USART1, (uint8_t) ch);
		
		/* 等待发送完毕 */
		while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);		
	
		return (ch);
}

///重定向c库函数scanf到串口，重写向后可使用scanf、getchar等函数
int fgetc(FILE *f)
{
		/* 等待串口输入数据 */
		while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);

		return (int)USART_ReceiveData(USART1);
}







